package com.df.bpm.utils.businessViewerV2.business;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.res.V_scrollPanel;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class V_scroll4HistoryBusinessDto extends V_scrollPanel<M_businessLocationsDTO4Subset> {

    V_businessByOutput baba;

    public V_scroll4HistoryBusinessDto(V_businessByOutput baba){
        this.baba=baba;
        this.putModel(null);
    }

    @Override
    public List<String> getColNames() {
        return Arrays.asList("业务ID", "序号","历史业务");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,22));
    }

    @Override
    public Object[] getRowValues(M_businessLocationsDTO4Subset row) {
        return new Object[]{row.getBusinessInformationIndex(),-1,row.getBusinessDesc()};
    }

    @Override
    public M_businessLocationsDTO4Subset getEmptyRow() {
        return new M_businessLocationsDTO4Subset();
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_businessLocationsDTO4Subset> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0 ) {
                    if (e.getClickCount() == 2) {// 处理双击事件
                        if(col==1){// 查询业务详情
                            M_businessLocationsDTO4Subset temp = list.get(row);
                            baba.setActiveBusiness(temp.getBusinessInformationIndex(),temp.getBusinessDesc());
                            while (list.size()>row+1){
                                list.remove(row+1);
                            }
                            putModel(list);
                        }
                    }
                }
            }
        };
    }

    public void insert(Integer businessId,String businessDesc){
        List<M_businessLocationsDTO4Subset> iModel=this.getModel();
        if(iModel.size()>1){
            if(Objects.equals(iModel.get(iModel.size() - 2).getBusinessInformationIndex(), businessId)){
                return;
            }
        }
        M_businessLocationsDTO4Subset temp=new M_businessLocationsDTO4Subset();
        temp.setBusinessInformationIndex(businessId);
        temp.setBusinessDesc(businessDesc);
        iModel.set(iModel.size()-1, temp);
        this.putModel(iModel);
    }

    public void reset() {
        this.putModel(null);
    }
}
